<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script src="{{ asset('bundles/gplusmain/js/jquery.min.js') }}" type="text/javascript"></script>
        <script src="{{ asset('bundles/gplusmain/js/twitStream.js') }}" type="text/javascript"></script>
        
        <link rel="stylesheet" href="{{ asset('bundles/gplusmain/css/twit-stream.css') }}" />
        
      <script>
      var lastGoogle;
      var lastTweet;

      function prependResults(items) {
          for (var i = items.length - 1; i >= 0; i--) {
          	var content = items[i].object.content;
              	if(content!=""){
  		        	var tTime=new Date(Date.parse(items[i].published));
  					var cTime=new Date();
  					var sinceMin=Math.round(((cTime-tTime)/60000)-60);
  					if(sinceMin==0){
  						var sinceSec=Math.round((cTime-tTime)/1000);
  						if(sinceSec<10)
  							var since='less than 10 seconds ago';
  						else if(sinceSec<20)
  							var since='less than 20 seconds ago';
  						else
  							var since='half a minute ago';
  					}
  					else if(sinceMin==1){
  						var sinceSec=Math.round((cTime-tTime)/1000);
  						if(sinceSec==30)
  							var since='half a minute ago';
  						else if(sinceSec<60)
  							var since='less than a minute ago';
  						else
  							var since='1 minute ago';
  					}
  					else if(sinceMin<45)
  						var since=sinceMin+' minutes ago';
  					else if(sinceMin>44&&sinceMin<60)
  						var since='about 1 hour ago';
  					else if(sinceMin<1440){
  						var sinceHr=Math.round(sinceMin/60);
  						if(sinceHr==1)
  							var since='about 1 hour ago';
  						else
  							var since='about '+sinceHr+' hours ago';
  					}
  					else if(sinceMin>1439&&sinceMin<2880)
  						var since='1 day ago';
  					else{
  						var sinceDay=Math.round(sinceMin/1440);
  						var since=sinceDay+' days ago';
  					}
  					
  					//if(content=="") content = items[i].attachments[1].content; 
  					var tweet='<div class="google all" data-date="'+tTime+'" data-id="'+items[i].id+'"><div class="google-left"><a target="_blank" href="'+items[i].actor.url+'"><img width="48" height="48" alt="'+items[i].actor.displayName+' on Google+" src="'+items[i].actor.image.url+'" /></a></div><div class="google-right"><p class="text">'+content+'<br /><a href="'+items[i].actor.url+'" target="_blank" class="google-user">+'+items[i].actor.displayName+'</a> </p><img src="{{ asset('bundles/gplusmain/images/plus.png') }}" width="50" style="float:right; margin-top:5px;"/></div><br style="clear: both;" /></div>';
  					$('#tweets').prepend(tweet);
  					
              	}
          }
          
        }
      
      function appendResults(items) {
        for (var i = items.length - 1; i >= 0; i--) {
        	var content = items[i].object.content;
            	if(content!=""){
		        	var tTime=new Date(Date.parse(items[i].published));
					var cTime=new Date();
					var sinceMin=Math.round(((cTime-tTime)/60000)-60);
					if(sinceMin==0){
						var sinceSec=Math.round((cTime-tTime)/1000);
						if(sinceSec<10)
							var since='less than 10 seconds ago';
						else if(sinceSec<20)
							var since='less than 20 seconds ago';
						else
							var since='half a minute ago';
					}
					else if(sinceMin==1){
						var sinceSec=Math.round((cTime-tTime)/1000);
						if(sinceSec==30)
							var since='half a minute ago';
						else if(sinceSec<60)
							var since='less than a minute ago';
						else
							var since='1 minute ago';
					}
					else if(sinceMin<45)
						var since=sinceMin+' minutes ago';
					else if(sinceMin>44&&sinceMin<60)
						var since='about 1 hour ago';
					else if(sinceMin<1440){
						var sinceHr=Math.round(sinceMin/60);
						if(sinceHr==1)
							var since='about 1 hour ago';
						else
							var since='about '+sinceHr+' hours ago';
					}
					else if(sinceMin>1439&&sinceMin<2880)
						var since='1 day ago';
					else{
						var sinceDay=Math.round(sinceMin/1440);
						var since=sinceDay+' days ago';
					}
					
					//if(content=="") content = items[i].attachments[1].content; 
					var google='<div class="google all" data-date="'+tTime+'" data-id="'+items[i].id+'"><div class="google-left"><a target="_blank" href="'+items[i].actor.url+'"><img width="48" height="48" alt="'+items[i].actor.displayName+' on Google+" src="'+items[i].actor.image.url+'" /></a></div><div class="google-right"><p class="text">'+content+'<br /><a href="'+items[i].actor.url+'" target="_blank" class="google-user">+'+items[i].actor.displayName+'</a> </p><img src="{{ asset('bundles/gplusmain/images/plus.png') }}" width="50" style="float:right; margin-top:5px;"/></div><br style="clear: both;" /></div>';
					$('#tweets').append(google);
					var googleActor = '<div class="actor"><a href="'+items[i].actor.url+'" target="_blank" class="google-user">+'+items[i].actor.displayName+'</a></div>';
					$('#contributors').append(googleActor);
					
            	}
        }
        
      }

      function makeRequest() {
        var request = gapi.client.plus.activities.search({
          'query': '{% block keyword1 %}gmaroc{% endblock %}',
          'maxResults': '20',
          'orderBy': 'recent'
        });
        request.execute(function(response) {
          console.log(response);
          appendResults(response.items);


          function sortDescending(a, b) {
     	     return a < b ? 1 : -1;
     	    };
			
        	 $(document).ready(init);

             function init() {
                 var parent = $('#tweets');
                 var children = $('.all', parent).get();
                 
                 children.sort(function(a, b) {
                    return sortDescending($(a).attr('data-date'),$(b).attr('data-date'));
                 })
                 $.each(children, function(i, child) {
                     parent.append(child);
                 });

                 var seen = {};
                 $('.actor').each(function() {
                     var txt = $(this).text();
                     if (seen[txt])
                         $(this).remove();
                     else
                         seen[txt] = true;
                 });

                 lastGoogle = $(".google").first().attr('data-id');
                 lastTweet = $(".tweet").first().attr('data-id');

                $("#countGoogle").text($("div[class='google all']").length);
   	          	$("#countTweet").text($("div[class='tweet all']").length);
   	          	$("#countAll").text($("div[class='tweet all']").length + $("div[class='google all']").length);
             }
        });
      }

      function load() {
        gapi.client.setApiKey('AIzaSyAR4S9DHf61HnlOpSxoRGnQetK4vZeJrnw');
        gapi.client.load('plus', 'v1', makeRequest);
      }

      function updateTweet(){
    	  var url="http://search.twitter.com/search.json?q={% block keyword2 %}gmaroc{% endblock %}&rpp=1&callback=?";
    		$.getJSON(url,function(json){
    			$(json.results).each(function(){
    				if(true){
    					var tTime=new Date(Date.parse(this.created_at));
    					var cTime=new Date();
    					var sinceMin=Math.round(((cTime-tTime)/60000)-60);
    					if(sinceMin==0){
    						var sinceSec=Math.round((cTime-tTime)/1000);
    						if(sinceSec<10)
    							var since='less than 10 seconds ago';
    						else if(sinceSec<20)
    							var since='less than 20 seconds ago';
    						else
    							var since='half a minute ago';
    					}
    					else if(sinceMin==1){
    						var sinceSec=Math.round((cTime-tTime)/1000);
    						if(sinceSec==30)
    							var since='half a minute ago';
    						else if(sinceSec<60)
    							var since='less than a minute ago';
    						else
    							var since='1 minute ago';
    					}
    					else if(sinceMin<45)
    						var since=sinceMin+' minutes ago';
    					else if(sinceMin>44&&sinceMin<60)
    						var since='about 1 hour ago';
    					else if(sinceMin<1440){
    						var sinceHr=Math.round(sinceMin/60);
    						if(sinceHr==1)
    							var since='about 1 hour ago';
    						else
    							var since='about '+sinceHr+' hours ago';
    					}
    					else if(sinceMin>1439&&sinceMin<2880)
    						var since='1 day ago';
    					else{
    						var sinceDay=Math.round(sinceMin/1440);
    						var since=sinceDay+' days ago';
    					}
    					var tweetBy='<a class="tweet-user" target="_blank" href="http://twitter.com/'+this.from_user+'">@'+this.from_user+'</a>';
    					if(showTweetLinks.indexOf('reply')!=-1)
    						tweetBy=tweetBy+' &middot; <a class="tweet-reply" target="_blank" href="http://twitter.com/?status=@'+this.from_user+' &in_reply_to_status_id='+this.id+'&in_reply_to='+this.from_user+'">Reply</a>';
    					if(showTweetLinks.indexOf('view')!=-1)
    						tweetBy=tweetBy+' &middot; <a class="tweet-view" target="_blank" href="http://twitter.com/'+this.from_user+'/statuses/'+this.id+'">View Tweet</a>';
    					if(showTweetLinks.indexOf('rt')!=-1)
    						tweetBy=tweetBy+' &middot; <a class="tweet-rt" target="_blank" href="http://twitter.com/?status=RT @'+this.from_user+' '+escape(this.text.replace(/&quot;/g,'"'))+'&in_reply_to_status_id='+this.id+'&in_reply_to='+this.from_user+'">RT</a>';
    					var tweet='<div class="tweet all" data-date="'+tTime+'" data-id="'+this.id+'"><div class="tweet-left"><a target="_blank" href="http://twitter.com/'+this.from_user+'"><img width="48" height="48" alt="'+this.from_user+' on Twitter" src="'+this.profile_image_url+'" /></a></div><div class="tweet-right"><p class="text">'+this.text.linkify().linkuser().linktag().replace(/<a/g,'<a target="_blank"')+'<br />'+tweetBy+'</p><img src="{{ asset('bundles/gplusmain/images/twitter.png') }}" width="50" style="float:right; margin-top:5px;"/></div><br style="clear: both;" /></div>';
    					if(lastTweet != this.id) {
        					$("#tweets").prepend(tweet);
        					lastTweet = this.id;
    					}
    					
    				}
    			});
    		});

          }

      function updateGoogle(){
    	        var request = gapi.client.plus.activities.search({
    	          'query': '{% block keyword3 %}gmaroc{% endblock %}',
    	          'maxResults': '1',
    	          'orderBy': 'recent'
    	        });
    	        request.execute(function(response) {
    	          console.log(response);
    	          if(lastGoogle != response.items[0].id){
        	           prependResults(response.items);
        	           lastGoogle = response.items[0].id;
    	          }

    	          $("#countGoogle").text($("div[class='google all']").length);
    	          $("#countTweet").text($("div[class='tweet all']").length);
    	          $("#countAll").text($("div[class='tweet all']").length + $("div[class='google all']").length);
    	        });
          }
      
      setInterval("updateTweet()",5000);
      setInterval("updateGoogle()",5000);

      
    	    
    </script>
    <script src="https://apis.google.com/js/client.js?onload=load"></script>
    
        <title>{% block title %}GTweet+{% endblock %}</title>
	</head>
	<body>
	   <div id="page">    
		<div id="header">
            {% block content_header %}                
               
            {% endblock %}
        </div>
        
        <div id="content">
            {% block content %}
            {% endblock %}
        </div>
    </body>
</html>